#include "cookiejar.h"

CookieJar *CookieJar::instance = new CookieJar();

CookieJar::CookieJar(QObject *parent) :
    QNetworkCookieJar(parent), settings("cookie.ini", QSettings::IniFormat)
{
  loadCookie();
}

#if 0
QList<QNetworkCookie>	CookieJar::cookiesForUrl( const QUrl & url ) const
{
  if (cookies.contains(url))
    return QList<QNetworkCookie>;
  else
    return cookies[url];
}

bool CookieJar::setCookiesFromUrl( const QList<QNetworkCookie> & cookieList, const QUrl & url )
{
  cookies[url] = cookieList;
}
#endif

void CookieJar::saveCookie()
{
  QList<QNetworkCookie> cookies = allCookies();
  QStringList value;

  for (int count = 0; count < cookies.length(); count++) {
    value.append(QString::fromUtf8(cookies[count].toRawForm().toBase64()));
  }

  settings.setValue("cookie", QVariant(value));
  settings.sync();
}

void CookieJar::loadCookie()
{
  if (settings.contains("cookie")) {
    QStringList list = settings.value("cookie").toStringList();
    QList<QNetworkCookie> cookies;

    for (int count = 0; count < list.length(); count++) {
      QList<QNetworkCookie> ck = QNetworkCookie::parseCookies(QByteArray::fromBase64(list[count].toUtf8()));
      cookies.append(ck);
    }

    setAllCookies(cookies);
  }
}

CookieJar *CookieJar::getInstance()
{
  return instance;
}
